from apps.orders.models import OrderInfo,OrderGoods
from rest_framework.serializers import ModelSerializer
from rest_framework import serializers
from apps.goods.models import SKU


class OrderInfoModelSerializer(ModelSerializer):
    """订单管理序列化器"""

    class Meta:
        model=OrderInfo
        fields=('order_id','create_time')


class SKUModelSerializer(ModelSerializer):
    """展示订单详细信息商品信息序列化器"""
    name=serializers.CharField(read_only=True)
    default_image_url=serializers.CharField(read_only=True,source='default_image')

    class Meta:
        model=SKU
        fields=('name','default_image_url')


class OrderGoodsModelSerializer(ModelSerializer):
    """商品详细序列化器"""
    sku=SKUModelSerializer()

    class Meta:
        model=OrderGoods
        fields=('count','price','sku')


class OrderInfoGoodsModelSerializer(ModelSerializer):
    """展示订单详细信息序列化器"""
    user=serializers.StringRelatedField()
    skus=OrderGoodsModelSerializer(many=True)

    class Meta:
        model=OrderInfo
        fields='__all__'

class OrderInfoUpdateModelSerializer(ModelSerializer):
    """更新订单状态序列化器"""

    class Meta:
        model = OrderInfo
        fields=('order_id','status')
        extra_kwargs = {
            'order_id': {'read_only':True} #write_only表明该字段仅用于反序列化输入。在序列化输出时忽略该字段。
        }
